//
//  HBReadSettingView.m
//  HeartBeat
//
//  Created by karong on 2023/9/12.
//

#import "HBReadSettingView.h"
#import "HBReadSettingManager.h"
@interface HBReadSettingView ()
@property (weak, nonatomic) IBOutlet UISlider *sliderView;
@property (weak, nonatomic) IBOutlet UIButton *reduceButton;
@property (weak, nonatomic) IBOutlet UIButton *plusButton;
@property (weak, nonatomic) IBOutlet UILabel *fontSizeLabel;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *spacingButtons;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *pageStyleButtons;
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *colorButtons;
@end

@implementation HBReadSettingView
- (void)awakeFromNib {
    [super awakeFromNib];
    HBReadSettingModel * model = [[HBReadSettingManager shared] showModel];
    self.sliderView.value = [UIScreen mainScreen].brightness;
    self.reduceButton.enabled = model.font > 12;
    self.plusButton.enabled = model.font < 30;
    self.fontSizeLabel.text = SF(@"%ld",(long)model.font);
    for (UIButton *button in self.colorButtons) {
        if (model.state == button.tag) {
            button.layer.borderColor = UIColor.font_yellow_FFCC00.CGColor;
        }
        else {
            button.layer.borderColor = UIColor.clearColor.CGColor;
        }
    }
    for (UIButton *button in self.pageStyleButtons) {
        if (button.tag == model.browseState) {
            button.layer.borderColor = UIColor.font_yellow_FFCC00.CGColor;
        }
        else {
            button.layer.borderColor = [UIColor colorFromHexRGB:@"cecece"].CGColor;
        }
    }
    for (UIButton *button in self.spacingButtons) {
        if (button.tag == model.lineSpacing) {
            button.layer.borderColor = UIColor.font_yellow_FFCC00.CGColor;
        }
        else {
            button.layer.borderColor = [UIColor colorFromHexRGB:@"cecece"].CGColor;
        }
    }
}

- (void)reloadAllViewStyle {
    
}

- (IBAction)brightnessChanged:(UISlider *)sender {
    [[UIScreen mainScreen] setBrightness:sender.value];
}

- (IBAction)colorAction:(UIButton *)sender {
    for (UIButton *button in self.colorButtons) {
        button.layer.borderColor = UIColor.clearColor.CGColor;
    }
    sender.layer.borderColor = UIColor.font_yellow_FFCC00.CGColor;
    [[HBReadSettingManager shared] setReadColorState:sender.tag];
    if ([self.delegate respondsToSelector:@selector(readSeetingWithSeetingView:changeSkinState:)]) {
        [self.delegate readSeetingWithSeetingView:self changeSkinState:sender.tag];
    }
}

- (IBAction)lineSpaceAction:(UIButton *)sender {
    for (UIButton *button in self.spacingButtons) {
        button.layer.borderColor = [UIColor colorFromHexRGB:@"cecece"].CGColor;
    }
    sender.layer.borderColor = UIColor.font_yellow_FFCC00.CGColor;
    [[HBReadSettingManager shared] setLineSpacing:sender.tag];
    if ([self.delegate respondsToSelector:@selector(readSeetingWithSeetingView:changeLineSpacing:)]) {
        [self.delegate readSeetingWithSeetingView:self changeLineSpacing:sender.tag];
    }
}

- (IBAction)pageStyleAction:(UIButton *)sender {
    for (UIButton *button in self.pageStyleButtons) {
        button.layer.borderColor = [UIColor colorFromHexRGB:@"cecece"].CGColor;
    }
    sender.layer.borderColor = UIColor.font_yellow_FFCC00.CGColor;
    [[HBReadSettingManager shared] setBrowseState:sender.tag];
    if ([self.delegate respondsToSelector:@selector(readSeetingWithSeetingView:changeBrowState:)]) {
        [self.delegate readSeetingWithSeetingView:self changeBrowState:sender.tag];
    }
}

- (IBAction)textSizeAction:(UIButton *)sender {
    HBReadSettingModel * model = [[HBReadSettingManager shared] showModel];
    NSInteger font = model.font;
    if (sender.tag > 0) {
        if (font < 30) {
            font += 1;
        }
    }
    else {
        if (font > 12) {
            font -= 1;
        }
    }
    self.reduceButton.enabled = font > 12;
    self.plusButton.enabled = font < 30;
    self.fontSizeLabel.text = SF(@"%ld", font);
    [[HBReadSettingManager shared] setFontSize:font];
    if ([self.delegate respondsToSelector:@selector(readSeetingWithSeetingView:changeBrowState:)]) {
        [self.delegate readSeetingWithSeetingView:self changeFont:font];
    }
}
- (IBAction)defaultTestSizeAction:(id)sender {
    self.fontSizeLabel.text = @"20";
    [[HBReadSettingManager shared] setFontSize:20];
    if ([self.delegate respondsToSelector:@selector(readSeetingWithSeetingView:changeBrowState:)]) {
        [self.delegate readSeetingWithSeetingView:self changeFont:20];
    }
}

@end
